package model;

import entity.Book;
import entity.Orders;
import entity.OrdersDetails;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import manager.Manager;

/**
 *
 * @author Noi
 */
public class BookInOrdersTableModel extends AbstractTableModel {
    
    private ArrayList<OrdersDetails> alOrdersDetails;
    private manager.Manager manager;
    
    public BookInOrdersTableModel(Manager manager) {
        this.alOrdersDetails = new ArrayList<OrdersDetails>();
        this.manager = manager;
    }
    
    public void setAlOrdersDetails(ArrayList<OrdersDetails> alOrdersDetails) {
        this.alOrdersDetails = alOrdersDetails;
        this.fireTableDataChanged();
    }
    
    @Override
    public int getRowCount() {
        return this.alOrdersDetails.size();
    }
    
    @Override
    public int getColumnCount() {
        return 3;
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        OrdersDetails details = this.alOrdersDetails.get(rowIndex);
        Orders orders = this.manager.getOrdersManager().getByOrdersID(details.getOrdersID());
        Book book = this.manager.getBookManager().getByBookID(details.getBookID());
        SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        
        switch (columnIndex) {
            case 0:
                return book;
            case 1:
                return df.format(orders.getIssueDate());
            case 2:
                return df.format(orders.getDueDate());
            default:
                return null;
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Book Name";
            case 1:
                return "Issue Date";
            case 2:
                return "Due Date";
            default:
                return null;
        }
    }
    
    public void add(OrdersDetails details) {
        this.alOrdersDetails.add(details);
        this.fireTableDataChanged();
    }
    
    public void remove(int index) {
        this.alOrdersDetails.remove(index);
        this.fireTableDataChanged();
    }
    
    public OrdersDetails get(int index) {
        return this.alOrdersDetails.get(index);
    }
}
